package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.WorkEnterprise;
import com.ruoyi.web.domain.vo.SysAreaVo;
import com.ruoyi.web.mapper.SysAreaMapper;
import com.ruoyi.web.mapper.WorkEnterpriseMapper;
import com.ruoyi.web.service.WorkEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-03-12
 */
@Service
public class WorkEnterpriseServiceImpl extends ServiceImpl<WorkEnterpriseMapper, WorkEnterprise> implements WorkEnterpriseService {

    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Override
    public List<String> getCompanyName(String companyName) {
//        List<WorkEnterprise> enterpriseList = this.list(new LambdaQueryWrapper<WorkEnterprise>().select(WorkEnterprise::getCompanyName).like(WorkEnterprise::getCompanyName, companyName));
//        List<String> companyNameList = enterpriseList.stream().map(WorkEnterprise::getCompanyName).collect(Collectors.toList());
        return this.baseMapper.companyNameList(companyName);
    }

    @Override
    public Map<String, String> getAreaByCompanyName(String companyName) {
        Map<String, String> resultMap = new HashMap<>();
        WorkEnterprise workEnterprise = this.getOne(new QueryWrapper<WorkEnterprise>().eq("company_name", companyName).select("province", "city", "county"));
        resultMap.put("province", workEnterprise.getProvince());
        resultMap.put("city", workEnterprise.getCity());
        resultMap.put("county", workEnterprise.getCounty());
        return resultMap;
    }

    /**
     * 查询公司的省市区编号和名称
     * @param companyName
     * @return
     */
    @Override
    public SysAreaVo getAreaInfoByCompanyName(String companyName) {
        SysAreaVo sysAreaVo = new SysAreaVo();
        WorkEnterprise workEnterprise = this.getOne(new QueryWrapper<WorkEnterprise>().eq("company_name", companyName).select("province", "city", "county"));
        String provinceCode = sysAreaMapper.queryCodeByName(workEnterprise.getProvince());
        String cityCode = sysAreaMapper.selectCodeByNameAndParentCode(workEnterprise.getCity(), provinceCode);
        String countyCode = sysAreaMapper.selectCodeByNameAndParentCode(workEnterprise.getCounty(), cityCode);
        sysAreaVo.setProvinceCode(provinceCode);
        sysAreaVo.setCityCode(cityCode);
        sysAreaVo.setCountyCode(countyCode);
        sysAreaVo.setProvinceName(workEnterprise.getProvince());
        sysAreaVo.setCityName(workEnterprise.getCity());
        sysAreaVo.setCountyName(workEnterprise.getCounty());
        return sysAreaVo;
    }

    /**
     * 查询省市区下拉列表
     * @param parentCode 父级编号
     * @return
     */
    public List<SysAreaVo> getAreaDictList(String parentCode) {
        if(StringUtils.isEmpty(parentCode)) {
            return new ArrayList<>();
        }
        List<SysAreaVo> list = sysAreaMapper.queryAreaByParentCode(parentCode);
        return list;
    }
}
